package weaver.interfaces.xuch.action;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.text.DecimalFormat;

/**
 * 合同开票流程 更新收款合同基本信息数据
 * 
 * 
 * @author 许畅
 * @Date 2020-11-4 10:22:46
 */
public class Action_hkkp_gygs implements Action {

	@Override
	public String execute(RequestInfo request) {
		// TODO Auto-generated method stub

		BaseBean bb = new BaseBean();
		String tablename = request.getRequestManager().getBillTableName();
		String requestid = request.getRequestid();
		bb.writeLog(">>>>>Action_hkkp_gygs>>>>>requestid" + requestid);
		RecordSet rs = new RecordSet();
		try {
			rs.executeQuery("select htbh,kplx,kpje from " + tablename + " where requestid=?", requestid);
			rs.next();
			String htbh = Util.null2String(rs.getString("htbh"));
			String kplx = Util.null2String(rs.getString("kplx"));
			double kpje = Util.getDoubleValue(rs.getString("kpje"), 0.00);

			rs.executeQuery("select id,ykp,sbfje1,jafje1,qtfje1,fwfykp,lxsrykp from uf_skhttzgygs where id = ?",htbh);
			rs.next();
			String id = Util.null2String(rs.getString("id"));
			double ykp = Util.getDoubleValue(rs.getString("ykp"), 0.00);//	已开票（元）
			double sbfje1 = Util.getDoubleValue(rs.getString("sbfje1"), 0.00);// 设备费金额（已开票）
			double jafje1 = Util.getDoubleValue(rs.getString("jafje1"), 0.00);// 建安费金额（已开票）
			double qtfje1 = Util.getDoubleValue(rs.getString("qtfje1"), 0.00);// 其他费金额（已开票）
			double fwfykp = Util.getDoubleValue(rs.getString("fwfykp"), 0.00);// 服务费(已开票)
			double lxsrykp = Util.getDoubleValue(rs.getString("lxsrykp"), 0.00);// 零星收入(已开票)
			// 如果没有结算金额 以合同金额为主 反之
			double jsje = Util.getDoubleValue(rs.getString("jsje"), 0.00);
			double htje = Util.getDoubleValue(rs.getString("htje"), 0.00);
			if (jsje == 0) {
				jsje = htje;
			}
			// 0 设备 1 建安 2 其他
			if (kplx.equals("0")) {
				sbfje1 += kpje;
			} else if (kplx.equals("1")) {
				jafje1 += kpje;
			} else if (kplx.equals("2")) {
				qtfje1 += kpje;
			}else if (kplx.equals("3")){
				fwfykp += kpje;
			}else if (kplx.equals("4")){
				lxsrykp += kpje;
			}
			ykp += kpje;
			String sqp = df(htje - ykp);// 尚欠款
			String kpbl1 = df(ykp / jsje);

			String update_sql = "update uf_skhttzgygs set qtfje1='" + df(qtfje1) + "',kpbl='" + kpbl1
					+ "',sqp='" + sqp + "',ykp='" + df(ykp) + "',sbfje1='" + df(sbfje1)
					+ "',jafje1='" + df(jafje1) + "',fwfykp='" + df(fwfykp)+ "',lxsrykp='" + df(lxsrykp)
					+ "' where id='" + id + "'";
			bb.writeLog(">>>>>Action_hkkp_gygs>>>>>sql=", update_sql);
			rs.executeUpdate(update_sql);

		} catch (Exception e) {
			bb.writeLog(">>>>>Action_hkkp_gygs>>>>>Exception=", e.toString());
			return Action.FAILURE_AND_CONTINUE;
		}
		return Action.SUCCESS;
	}
	public String df(double je) {
		DecimalFormat decimalFormat = new DecimalFormat("0.00");
		new BaseBean().writeLog("je::"+je);
		if ((je + "").indexOf(".") != -1) {
			if ((je + "").split("\\.")[1].length() == 1) {
				decimalFormat = new DecimalFormat("0.0");
			}
		}
		return decimalFormat.format(je);
	}

}
